﻿using System;
using System.Collections.Generic;
using System.Data;

namespace YJKRevitFamilyDataBase
{
	public class MySqlSubjectTable : ISubjectTable
	{
		public MySqlSubjectTable(MySqlDatabase db)
		{
			this.mDatabase = db;
		}

		public DbSubject this[Guid id]
		{
			get
			{
				if (this.mDatabase.Connection.State == ConnectionState.Closed)
				{
					this.mDatabase.Connection.Open();
				}
				DataTable dataTable = this.mDatabase.ExecQuery(string.Format("select SubjectName,`Version` from SubjectTable where GUID='{0}'", id.ToString()));
				if (this.mDatabase.Connection.State == ConnectionState.Open)
				{
					this.mDatabase.Connection.Close();
				}
				if (dataTable != null)
				{
					DbSubject dbSubject = new DbSubject(dataTable.Rows[0][0].ToString().Trim(), id);
					dbSubject.Version = dataTable.Rows[0][1].ToString();
					dbSubject.SetDatabase(this.mDatabase);
					return dbSubject;
				}
				return null;
			}
		}

		public List<DbSubject> AllSubject()
		{
			if (this.mDatabase.Connection.State == ConnectionState.Closed)
			{
				this.mDatabase.Connection.Open();
			}
			List<DbSubject> list = new List<DbSubject>();
			DataTable dataTable = this.mDatabase.ExecQuery(string.Format("select SubjectName,`Version`,GUID from SubjectTable", Array.Empty<object>()));
			if (this.mDatabase.Connection.State == ConnectionState.Open)
			{
				this.mDatabase.Connection.Close();
			}
			if (dataTable != null)
			{
				for (int i = 0; i < dataTable.Rows.Count; i++)
				{
					DbSubject dbSubject = new DbSubject(dataTable.Rows[i][0].ToString().Trim(), new Guid(dataTable.Rows[i][2].ToString()));
					dbSubject.Version = dataTable.Rows[i][1].ToString();
					dbSubject.SetDatabase(this.mDatabase);
					list.Add(dbSubject);
				}
			}
			return list;
		}

		public SubjectType SubjectType
		{
			get
			{
				string text = this.AllSubject()[0].Guid.ToString().Trim().ToLower();
				uint num = YArchitech.Revit.PrivateImplementationDetails.ComputeStringHash(text);
				if (num <= 1650085118u)
				{
					if (num != 53210732u)
					{
						if (num != 501046314u)
						{
							if (num == 1650085118u)
							{
								if (text == "8b212901-9f74-43a0-8cee-3ceae00ef944")
								{
									return SubjectType.Company;
								}
							}
						}
						else if (text == "d27c7e1b-40cd-4b2f-83e2-c5c14b98a80a")
						{
							return SubjectType.Architecture;
						}
					}
					else if (text == "1c7d0e47-274a-4aee-9827-866f6c62719a")
					{
						return SubjectType.System;
					}
				}
				else if (num <= 2060259847u)
				{
					if (num != 1847185643u)
					{
						if (num == 2060259847u)
						{
							if (text == "28bd20ce-8cc7-474c-a21b-db084ad93f77")
							{
								return SubjectType.HVAC;
							}
						}
					}
					else if (text == "cc9eeaee-4273-4406-a35b-7903804829ad")
					{
						return SubjectType.Plumbing;
					}
				}
				else if (num != 2623590755u)
				{
					if (num == 2897540702u)
					{
						if (text == "3c5d6e47-274a-4aee-9827-866f6c62719a")
						{
							return SubjectType.Project;
						}
					}
				}
				else if (text == "be10f82f-915a-4e0f-99d0-8f35b568e7aa")
				{
					return SubjectType.Electric;
				}
				return SubjectType.Architecture;
			}
		}

		public string GetVersion()
		{
			if (this.mDatabase.Connection.State == ConnectionState.Closed)
			{
				this.mDatabase.Connection.Open();
			}
			string result = "";
			string sqlStr = string.Format("select `Version` from SubjectTable", Array.Empty<object>());
			DataTable dataTable = this.mDatabase.ExecQuery(sqlStr);
			if (this.mDatabase.Connection.State == ConnectionState.Open)
			{
				this.mDatabase.Connection.Close();
			}
			if (dataTable != null && dataTable.Rows.Count > 0)
			{
				result = dataTable.Rows[0]["Version"].ToString();
			}
			return result;
		}

		public void SetVersion(string ver)
		{
			if (this.mDatabase.Connection.State == ConnectionState.Closed)
			{
				this.mDatabase.Connection.Open();
			}
			string sqlStr = string.Format("UPDATE SubjectTable SET `Version` = '{0}'", ver);
			this.mDatabase.ExecQuery(sqlStr);
			if (this.mDatabase.Connection.State == ConnectionState.Open)
			{
				this.mDatabase.Connection.Close();
			}
		}

		private MySqlDatabase mDatabase;
	}
}
